package com.union.core.doc.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.googlecode.jsonplugin.annotations.JSON;
import com.hnjowe.util.ComUtil;
import com.hnjowe.util.TxtReader;
import com.hnjowe.util.TxtWriter;
import com.mysql.jdbc.StringUtils;
import com.opensymphony.xwork2.Action;
import com.union.core.IConstant;
import com.union.core.action.ParamAction;
import com.union.core.doc.model.DocAttachment;
import com.union.core.doc.model.DocTemplate;
import com.union.core.doc.model.OfficialDoc;
import com.union.core.doc.model.OfficialDocOrg;
import com.union.core.doc.model.SealSignature;
import com.union.core.doc.pojo.OfficialDocOrgPojo;
import com.union.core.doc.pojo.OfficialDocPojo;
import com.union.core.doc.service.OfficialDocManagerService;
import com.union.core.model.TreeAttr;
import com.union.core.model.TreeItem;
import com.union.core.organization.model.OrganizationManager;
import com.union.core.organization.model.OrganizationParam;
import com.union.core.organization.service.OrganizationManagerService;
import com.union.core.organization.service.OrganizationParamService;
import com.union.core.sys.service.SysLogsService;

/**
 * 公文管理共用action
 * @author leochen
 *
 */
public class OfficialDocManagerAction extends ParamAction{
	private static final long serialVersionUID = 2903757015424284430L;
	
	private static Logger log = Logger.getLogger(OfficialDocManagerAction.class.getName());
	
	private OfficialDocManagerService officialDocService;
	private OfficialDoc officialDoc;
	private OfficialDoc officialDoc1;
	private OfficialDocOrg officialDocOrg;
	private OfficialDocPojo officialDocPojo;
	private OfficialDocOrgPojo officialDocOrgPojo;
	private List<OfficialDoc> officialDocList;
	/**
	 * 单位接收公文列表
	 */
	private List<OfficialDocOrg> officialDocOrgList;
	private List<OfficialDocOrg> officialDocOrgList1;
	private List<OfficialDocOrg> officialDocOrgList2;
	private List<OfficialDocOrg> officialDocOrgList3;
	private OrganizationManagerService organizationService;
	private String selSection;//已选择的部门
	private List<SealSignature> sealSignatureList;
	private SealSignature sealSignature;
	private List<DocTemplate> docTemplateList;
	private DocTemplate docTemplate;
	private DocAttachment docAttachment;
	
	private int orgid;
	
	private long docAttachId;
	
	private long docTemplateId;
	
	private long send_userid;
	
	private List<TreeItem> orgList;
	
	private String ajaxMsg;
	
	private String receive_section;
	
	private OrganizationParamService ParamService;
	private List<OrganizationParam> ParamList1;
	private List<OrganizationParam> ParamList2;
	private List<OrganizationParam> ParamList3;
	private List<OrganizationParam> ParamList4;
	
	/**
	 * 公章上传目录
	 */
	private final static String SEAL_UPLOAD_DIR = "/upload/seal/";
	
	/**
	 * 附件上传目录
	 */
	private final static String ATTACH_UPLOAD_DIR = "/upload/";
	
	/**
	 * 头文件上传目录
	 */
	private final static String TEMPLATE_UPLOAD_DIR = "/upload/template/";
	//日志
	private SysLogsService logsSer;
	//操作权限
	private List<String> allPower;
	
	List<OrganizationManager> forglist;
	List<OrganizationManager> threelist;
	private OrganizationParam param;
	private int grade;
	private OrganizationManager orgManager; 

	public OfficialDocManagerAction(){
		officialDocService = new OfficialDocManagerService();
		officialDoc= new OfficialDoc();
		officialDoc1= new OfficialDoc();
		officialDocOrg = new OfficialDocOrg();
		officialDocPojo = new OfficialDocPojo();
		officialDocOrgPojo = new OfficialDocOrgPojo();
		officialDocList = new ArrayList<OfficialDoc>();
		organizationService = new OrganizationManagerService();
		sealSignature = new SealSignature();
		docTemplate = new DocTemplate();
		logsSer = new SysLogsService();
		ParamService = new OrganizationParamService();
		forglist = new ArrayList<OrganizationManager>();
		threelist = new ArrayList<OrganizationManager>();
		param = new OrganizationParam();
		docAttachment = new DocAttachment();
		orgManager = new OrganizationManager();
	}
	
	public String goPublish(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(officialDocPojo.getId() > 0){
			officialDoc = officialDocService.query(officialDocPojo.getId());
			officialDocPojo.copy(officialDoc);
			officialDocPojo.setDocAttachList(officialDocService.queryDocAttach(officialDoc.getId()));
		}
		sealSignatureList = officialDocService.querySeal(sealSignature, 0, 0);
		docTemplateList = officialDocService.queryTemplate(docTemplate, 0, 0);
//		ParamList1 = ParamService.queryList(1);
//		ParamList2 = ParamService.queryList(2);
//		ParamList3 = ParamService.queryList(3);
//		ParamList4 = ParamService.queryList(4);
		param = ParamService.query(sysService.getSessionListener().getPERSONNEL().getOrganization_id());
		grade = param.getGrade();
		forglist = organizationService.queryChildren(sysService.getSessionListener().getPERSONNEL().getOrganization_id());
		threelist = organizationService.queryparentid(sysService.getSessionListener().getPERSONNEL().getPid());
		return "goPublish";
	}
	
	public String save(){
		String result = "goPublish";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			sealSignatureList = officialDocService.querySeal(sealSignature, 0, 0);
			docTemplateList = officialDocService.queryTemplate(docTemplate, 0, 0);
			int curUserid = sysService.getSessionListener().getMANAGER_ID();
			Date sysDate = new Date();
			if(officialDocPojo.getId() <= 0){//新增
				officialDoc.copy(officialDocPojo);
				officialDoc.setReceive_section(receive_section);
				officialDoc.setCreate_userid(curUserid);
				officialDoc.setCreate_time(sysDate);
				officialDocService.add(officialDoc);
				officialDocPojo.setId(officialDoc.getId());
				setMsgInfo("新增公文成功！");
				logsSer.addSysLogs("新增公文【"+officialDocPojo.getTitle()+"】");
			}else{//修改
				
				if(officialDoc == null){
					setMsgInfo("无法找到公文！");
					return result;
				}
				officialDoc.copy(officialDocPojo);
				officialDoc.setReceive_section(receive_section);
				officialDoc.setModify_userid(curUserid);
				officialDoc.setModify_time(sysDate);
				officialDocService.edit(officialDoc);
				setMsgInfo("修改公文成功！");
				logsSer.addSysLogs("修改公文【"+officialDocPojo.getTitle()+"】");
			}
			officialDoc = officialDocService.query(officialDocPojo.getId());

			dealAttach(curUserid, sysDate);
			officialDocPojo.setDocAttachList(officialDocService.queryDocAttach(officialDoc.getId()));
//			ParamList1 = ParamService.queryList(1);
//			ParamList2 = ParamService.queryList(2);
//			ParamList3 = ParamService.queryList(3);
//			ParamList4 = ParamService.queryList(4);
			param = ParamService.query(sysService.getSessionListener().getPERSONNEL().getOrganization_id());
			grade = param.getGrade();
			forglist = organizationService.queryChildren(sysService.getSessionListener().getPERSONNEL().getOrganization_id());
		}
		return result;
	}
	
	private List<DocAttachment> dealAttach(int curUserid, Date sysDate){
		List<DocAttachment> daList = new ArrayList<DocAttachment>();
		//上传附件
		if(officialDocPojo.getAttach() != null && officialDocPojo.getAttach().length > 0){
			String fileName = "";
			File file = null;
			DocAttachment da = null;
			for(int i = 0;i!=officialDocPojo.getAttach().length; ++i){
				file = officialDocPojo.getAttach()[i];
				if(file != null){
					fileName = ComUtil.genFileId();
					String fileext = ComUtil.getExtention(officialDocPojo.getAttachFileName()[i]);
					fileName = fileName + fileext;
					
					String fullPath = getRealPath() + ATTACH_UPLOAD_DIR + fileName;
					ComUtil.copyFile(file, new File(fullPath));
					
					da = new DocAttachment();
					da.setDoc_id(officialDoc.getId());
					da.setAttach_title(officialDocPojo.getAttach_title()[i]);
					da.setAttach_name(fileName);
					da.setAttach_old_name(officialDocPojo.getAttachFileName()[i]);
					da.setAttach_type(officialDocPojo.getAttachContentType()[i]);
					da.setAttach_path(ATTACH_UPLOAD_DIR + fileName);
					da.setCreate_userid(curUserid);
					da.setCreate_time(sysDate);
					daList.add(da);
				}
			}
			officialDocService.add(daList);
		}
		return daList;
	}

	@JSON
	public String getOrg(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			orgList = new ArrayList<TreeItem>();
			orgList.addAll(getTreeItem(orgid));
			return Action.SUCCESS;
		}
	}
	
	private List<TreeItem> getTreeItem(int pid){
		List<TreeItem> result = new ArrayList<TreeItem>();
		List<OrganizationManager> myOrgList = organizationService.queryList(pid);
		if(myOrgList != null && myOrgList.size() > 0){
			TreeItem ti;
			TreeAttr ta;
			int orgCount = 0;
			for(OrganizationManager om : myOrgList){
				orgCount = organizationService.ListCount(om.getOrganization_id());
				
				ti = new TreeItem();
				ti.setData(om.getOrganization_name());
				ti.setState(orgCount > 0 ? "closed" : "");
				if(orgCount > 0){
					ti.setChildren(getTreeItem(om.getOrganization_id()));
				}
				
				ta = new TreeAttr();
				ta.setId(om.getOrganization_id());
				ta.setHasChild(orgCount > 0 ? true : false);
				if(checkSel(selSection, om.getOrganization_id())){
					ta.setCheckedstate("checked");
				}else{
					ta.setCheckedstate("unchecked");
				}
				
				ti.setAttr(ta);
				result.add(ti);
			}
		}
		return result;
	}
	
	private boolean checkSel(String selSection, int orgid){
		if(selSection != null && selSection.length() > 0){
			String[] sels = selSection.split(",");
			for(String sel : sels){
				if(sel.equals(String.valueOf(orgid))){
					return true;
				}
			}
		}
		return false;
	}
	
	public String send(){
		String result = "goPublish";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			sealSignatureList = officialDocService.querySeal(sealSignature, 0, 0);
			docTemplateList = officialDocService.queryTemplate(docTemplate, 0, 0);
			int curUserid = sysService.getSessionListener().getMANAGER_ID();
			Date sysDate = new Date();
			if(officialDocPojo.getId() <= 0){//新增
				officialDoc.copy(officialDocPojo);
				officialDoc.setReceive_section(receive_section);
				officialDoc.setCreate_userid(curUserid);
				officialDoc.setCreate_time(sysDate);
				officialDoc.setFlow_flag("1");
				officialDoc.setOpt_send_time(sysDate);
				officialDocService.add(officialDoc);
				officialDocPojo.setId(officialDoc.getId());

				officialDoc = officialDocService.query(officialDocPojo.getId());
				
				
				officialDocPojo.setDocAttachList(dealAttach(curUserid, sysDate));
				officialDoc1 = officialDocService.query(officialDocPojo.getId());
				//根据所选机构下发
				if(officialDoc1.getReceive_section() != null){
					String[] receiveSections = officialDoc1.getReceive_section().split(",");
					if(receiveSections.length == 0){
						setMsgInfo("请选择下发单位！");
						return result;
					}
					int receiveId = 0;
					OfficialDocOrg ooo = null;
					for(String section : receiveSections){
						if(section!=""){
							receiveId = Integer.parseInt(section);
							ooo = new OfficialDocOrg();
							ooo.setDoc_id(officialDoc1.getId());
							ooo.setOrg_id(receiveId);
							ooo.setSend_userid(curUserid);
							ooo.setSend_time(sysDate);
							
							officialDocService.add(ooo);
						}
					}
					
				}else{
					setMsgInfo("请选择下发单位！");
					return result;
				}
				officialDocPojo = new OfficialDocPojo();
				setMsgInfo("下发公文成功！");
				logsSer.addSysLogs("下发公文【"+officialDocPojo.getTitle()+"】");
				
//				ParamList1 = ParamService.queryList(1);
//				ParamList2 = ParamService.queryList(2);
//				ParamList3 = ParamService.queryList(3);
//				ParamList4 = ParamService.queryList(4);
				param = ParamService.query(sysService.getSessionListener().getPERSONNEL().getOrganization_id());
				grade = param.getGrade();
				forglist = organizationService.queryChildren(sysService.getSessionListener().getPERSONNEL().getOrganization_id());
			}
				return result;
		}
	}
	
	public String goPublishList(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		int curUserid = sysService.getSessionListener().getMANAGER_ID();
		intRowCount = officialDocService.queryCount(officialDocPojo,curUserid);
		calPage();
		if(intPageCount>0){
			officialDocList = officialDocService.query(officialDocPojo, start, intPageSize,curUserid);
			
			//处理参数名称
			for(OfficialDoc od : officialDocList){
				od.setDoc_type_name(getParamName(od.getDoc_type(), IConstant.FILE_TYPE));
				od.setEmergency_level_name(getParamName(od.getEmergency_level(), IConstant.EMERGENCY_LEVEL));
				od.setConfidential_level_name(getParamName(od.getConfidential_level(), IConstant.SECRET_LEVEL));
				od.setFlow_flag_name(getParamName(od.getFlow_flag(), IConstant.DOC_FLOW_STATUS));
			}
		}
		return "goPublishList";
	}
	
	public String delDoc(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(officialDocPojo.getId() > 0){
			officialDocService.delete(officialDocPojo.getId());
			setMsgInfo("删除公文成功！");
			logsSer.addSysLogs("删除公文【"+officialDocPojo.getTitle()+"】");
		}
		return goPublishList();
	}
	
	public String publishView(){
		String result = "publishView";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(officialDocPojo.getId() > 0){
			officialDoc = officialDocService.query(officialDocPojo.getId());
			if(officialDoc == null){
				setMsgInfo("无法找到公文！");
				return result;
			}
			officialDocPojo.copy(officialDoc);
			officialDocPojo.setDocAttachList(officialDocService.queryDocAttach(officialDoc.getId()));
		}
		return result;
	}
	
	public String viewSignStatus(){
		String result = "viewSignStatus";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		intRowCount = officialDocService.queryDocOrgForStatusCount(officialDocOrgPojo);
		calPage();
		if(intPageCount>0){
			officialDocOrgPojo.setSign_status("0");
			officialDocOrgList1 = officialDocService.queryDocOrgForStatus(officialDocOrgPojo, start, intPageSize);
			officialDocOrgPojo.setSign_status("1");
            officialDocOrgList2 = officialDocService.queryDocOrgForStatus(officialDocOrgPojo, start, intPageSize);
			officialDocOrgPojo.setSign_status("2");
            officialDocOrgList3 = officialDocService.queryDocOrgForStatus(officialDocOrgPojo, start, intPageSize);
			//处理参数名称
//			for(OfficialDocOrg odo : officialDocOrgList1){
//				odo.setSign_status_name(getParamName(odo.getSign_status(), IConstant.DOC_SIGN_WILL));
//			}
//			for(OfficialDocOrg odo : officialDocOrgList2){
//				odo.setSign_status_name(getParamName(odo.getSign_status(), IConstant.DOC_SIGN_DONE));
//			}
		}
		return result;
	}
	
	public String goSign(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(sysService.getSessionListener().getSysManager().getPERSONNEL() != null){
			int orgId = sysService.getSessionListener().getSysManager().getPERSONNEL().getOrganization_id();
			officialDocOrgPojo.setOrg_id(orgId);
			officialDocOrgPojo.setSign_status(IConstant.DOC_SIGN_WILL);
			officialDocOrgPojo.setSend_userid(send_userid);
			officialDocOrgList = officialDocService.queryDocOrg(officialDocOrgPojo, start, intPageSize);
			allPower=sysService.getSessionListener().getAllPower();
			//intRowCount = officialDocService.queryDocOrgCount(officialDocOrgPojo);
//			calPage();
//			if(intPageCount>0){
				
//			}
		}
		return "goSign";
	}
	
	public String goSigned(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(sysService.getSessionListener().getSysManager().getPERSONNEL() != null){
			int orgId = sysService.getSessionListener().getSysManager().getPERSONNEL().getOrganization_id();		
			officialDocOrgPojo.setOrg_id(orgId);
			officialDocOrgPojo.setSign_status(IConstant.DOC_SIGN_DONE);
			
			intRowCount = officialDocService.queryDocOrgCount(officialDocOrgPojo);
			calPage();
			if(intPageCount>0){
				officialDocOrgList = officialDocService.queryDocOrg(officialDocOrgPojo, start, intPageSize);
				System.out .println("officialDocOrgList.size()"+officialDocOrgList.size());
				System.out .println("officialDocOrgList"+officialDocOrgList.get(0).getOfficialDoc().getOrgManager().getOrganization_name());
			}
		}
		return "goSigned";
	}
	public String goOver(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(sysService.getSessionListener().getSysManager().getPERSONNEL() != null){
			int orgId = sysService.getSessionListener().getSysManager().getPERSONNEL().getOrganization_id();		
			officialDocOrgPojo.setOrg_id(orgId);
			officialDocOrgPojo.setSign_status(IConstant.DOC_SIGN_EVEN);
			
			intRowCount = officialDocService.queryDocOrgCount(officialDocOrgPojo);
			calPage();
			if(intPageCount>0){
				officialDocOrgList = officialDocService.queryDocOrg(officialDocOrgPojo, start, intPageSize);
				System.out .println("officialDocOrgList.size()"+officialDocOrgList.size());
				System.out .println("officialDocOrgList"+officialDocOrgList.get(0).getOfficialDoc().getOrgManager().getOrganization_name());
			}
		}
		return "goOver";
	}
	public String signView(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
	
		if(officialDocOrgPojo.getDoc_id() > 0){
			officialDocOrg = officialDocService.queryOrg(officialDocOrgPojo.getId());	
			//officialDocOrg = officialDocService.queryOrg(officialDocOrgPojo.getId());
			int orgId = sysService.getSessionListener().getSysManager().getPERSONNEL().getOrganization_id();
			officialDocOrgPojo.setOrg_id(orgId);
			officialDocOrgPojo.setSign_status(IConstant.DOC_SIGN_DONE);
			docAttachment.setAttach_path(IConstant.DOC_SIGN_DONE);
			intRowCount = officialDocService.queryDocOrgCount(officialDocOrgPojo);
			calPage();
			officialDoc = officialDocService.query(officialDocOrgPojo.getDoc_id());
			if(intPageCount>0){
				officialDocOrgList = officialDocService.queryDocOrg(officialDocOrgPojo, start, intPageSize);
			}
			
			officialDocPojo.copy(officialDoc);
			officialDocPojo.setDocAttachList(officialDocService.queryDocAttach(officialDoc.getId()));
			officialDocOrg.setSign_userid(sysService.getSessionListener().getMANAGER_ID());
			
			
		}
			
			
			return "signView";
		
	}
	
	public String signViewFile(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		try{
			if(officialDocPojo.getId() > 0){
				officialDoc = officialDocService.query(officialDocPojo.getId());
				officialDocPojo.copy(officialDoc);
				
				String templateFileName = getParamStr1(officialDoc.getTemplate_id(), IConstant.OFFICIAL_DOC_TEMPLATE);
				String htmlName = ComUtil.genFileId() + ".doc";
				String templatePath = getTemplatePath();
				String tempPath = getTempPath();
				String templateFullPath = templatePath + templateFileName;
				String tempFullPath = tempPath + htmlName;
				File docFile = new File(templateFullPath);
				String docContent = TxtReader.loadStringFromFile(docFile);
				docContent = docContent.replaceAll("\\{title\\}", officialDoc.getTitle());
				docContent = docContent.replaceAll("\\{doc_no\\}", officialDoc.getDoc_no());
				docContent = docContent.replaceAll("\\{content\\}", officialDoc.getContent());
				File newFile = new File(tempFullPath);
				TxtWriter.saveToFile(docContent, newFile);
				officialDocPojo.setHtmlFileName(htmlName);
			}
		}catch(Exception e){
			log.error(e);
			e.printStackTrace();
		}
		return "signViewFile";
	}
	
	public String doSign(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(officialDocOrgPojo.getId() > 0){
			
			officialDocOrg = officialDocService.queryOrg(officialDocOrgPojo.getId());
			if(IConstant.DOC_SIGN_WILL.equals(officialDocOrg.getSign_status())){
			officialDocOrg.setSign_userid(sysService.getSessionListener().getMANAGER_ID());
			officialDocOrg.setSign_time(new Date());
			
			officialDocOrg.setSign_status(IConstant.DOC_SIGN_DONE);
			officialDocService.edit(officialDocOrg);
			//setMsgInfo("签收公文成功！");
			logsSer.addSysLogs("签收公文【"+officialDocPojo.getTitle()+"】");
			}
		}else{
			setMsgInfo("请选择要签收的公文！");
		}
		return goSign();
	}
	
	/**办理公文
	 * @return
	 */
	public String doOver(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(officialDocOrgPojo.getId() > 0){
			
			officialDocOrg = officialDocService.queryOrg(officialDocOrgPojo.getId());
			
			officialDocOrg.setSign_status(IConstant.DOC_SIGN_EVEN);
			officialDocService.edit(officialDocOrg);
		}else{
			setMsgInfo("请选择要办理公文！");
		}
		return goOver();
	}
	public String goSeal(){
		String reslut = "goSeal";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		allPower=sysService.getSessionListener().getAllPower();
		officialDocOrg.setSign_status(IConstant.DOC_SIGN_DONE);
		intRowCount = officialDocService.querySealCount(sealSignature);
		calPage();
		if(intPageCount>0){
			sealSignatureList = officialDocService.querySeal(sealSignature, start, intPageSize);
			
			//处理参数名称
			for(SealSignature ss : sealSignatureList){
				ss.setSeal_type_name(getParamName(ss.getSeal_type(), IConstant.SEAL_TYPE));
			}
		}
		return reslut;
	}
	
	public String goSealEdit(){
		String reslut = "goSealEdit";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(sealSignature.getId() > 0){
			sealSignature = officialDocService.querySeal(sealSignature.getId());
		}
		return reslut;
	}
	
	public String doSealEdit(){
		String reslut = "goSealEdit";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}

		String fileName = "";
		//上传文件
		if(sealSignature.getSealFile() != null){
			fileName = ComUtil.genFileId();
			String fileext = ComUtil.getExtention(sealSignature.getSealFileFileName());
			fileName = fileName + fileext;
			
			String fullPath = getRealPath() + SEAL_UPLOAD_DIR + fileName;
			ComUtil.copyFile(sealSignature.getSealFile(), new File(fullPath));
			
			sealSignature.setFile_name(fileName);
			sealSignature.setFile_path(SEAL_UPLOAD_DIR + fileName);
			sealSignature.setFile_type(sealSignature.getSealFileContentType());
		}
		if(sealSignature.getId() == 0){
			sealSignature.setCreate_userid(sysService.getSessionListener().getMANAGER_ID());
			sealSignature.setCreate_time(new Date());
			officialDocService.add(sealSignature);
			this.setMsgInfo("新增公章成功！");
			logsSer.addSysLogs("新增公章【"+fileName+"】路径【"+SEAL_UPLOAD_DIR + fileName+"】");
		}else{
			SealSignature oldSealSignature = officialDocService.querySeal(sealSignature.getId());
			oldSealSignature.copy(sealSignature);
			officialDocService.edit(oldSealSignature);
			sealSignature = oldSealSignature;
			this.setMsgInfo("修改公章成功！");
			logsSer.addSysLogs("修改公章【"+fileName+"】路径【"+SEAL_UPLOAD_DIR + fileName+"】");
		}
		return reslut;
	}
	
	public String delSeal(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(sealSignature.getId() > 0){
			officialDocService.deleteSeal(sealSignature.getId());
			setMsgInfo("删除公章成功！");
			logsSer.addSysLogs("删除公章【"+sealSignature.getFile_name()+"】路径【"+sealSignature.getFile_path()+"】");
		}
		return goSeal();
	}
	
	@JSON
	public String delDocAttach(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			if(docAttachId > 0){
				DocAttachment da = officialDocService.queryDAById(docAttachId);
				if(da != null){
					//删除文件
					File file = new File(getRealPath() + da.getAttach_path());
					if(file.exists()){
						file.delete();
					}
				}
				
				officialDocService.deleteDocAttach(docAttachId);
				ajaxMsg = "删除成功！";
				logsSer.addSysLogs("删除公文附件【"+da.getAttach_old_name()+"】路径【"+getRealPath() + da.getAttach_path()+"】");

			}else{
				ajaxMsg = "要删除的附件ID不能为空！";
			}
			return Action.SUCCESS;
		}
	}
	
	public String goTemplate(){
		String reslut = "goTemplate";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		allPower=sysService.getSessionListener().getAllPower();
		intRowCount = officialDocService.queryTemplateCount(docTemplate);
		calPage();
		if(intPageCount>0){
			docTemplateList = officialDocService.queryTemplate(docTemplate, start, intPageSize);
			
			//处理参数名称
			for(DocTemplate dt : docTemplateList){
				dt.setTemplate_type_name(getParamName(dt.getTemplate_type(), IConstant.TEMPLATE_TYPE));
			}
		}
		return reslut;
	}
	
	public String goTemplateEdit(){
		String reslut = "goTemplateEdit";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(docTemplate.getId() > 0){
			docTemplate = officialDocService.queryTemplate(docTemplate.getId());
		}
		return reslut;
	}
	
	public String doTemplateEdit(){
		String reslut = "goTemplateEdit";
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}

		String fileName = "";
		//上传文件
		if(docTemplate.getTemplateFile() != null){
			fileName = ComUtil.genFileId();
			String fileext = ComUtil.getExtention(docTemplate.getTemplateFileFileName());
			fileName = fileName + fileext;
			
			String fullPath = getRealPath() + TEMPLATE_UPLOAD_DIR + fileName;
			ComUtil.copyFile(docTemplate.getTemplateFile(), new File(fullPath));
			
			docTemplate.setFile_name(fileName);
			docTemplate.setFile_path(TEMPLATE_UPLOAD_DIR + fileName);
			docTemplate.setFile_type(docTemplate.getTemplateFileContentType());
		}
		if(docTemplate.getId() == 0){
			docTemplate.setCreate_userid(sysService.getSessionListener().getMANAGER_ID());
			docTemplate.setCreate_time(new Date());
			officialDocService.add(docTemplate);
			this.setMsgInfo("新增头文件成功！");
			logsSer.addSysLogs("新增头文件【"+fileName+"】路径【"+TEMPLATE_UPLOAD_DIR + fileName+"】");

		}else{
			DocTemplate oldDocTemplate = officialDocService.queryTemplate(docTemplate.getId());
			oldDocTemplate.copy(docTemplate);
			officialDocService.edit(oldDocTemplate);
			docTemplate = oldDocTemplate;
			this.setMsgInfo("修改头文件成功！");
			logsSer.addSysLogs("修改头文件【"+oldDocTemplate.getFile_name()+"】路径【"+oldDocTemplate.getFile_path()+"】");
		}
		return reslut;
	}
	
	public String delTemplate(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(docTemplate.getId() > 0){
			officialDocService.deleteTemplate(docTemplate.getId());
			setMsgInfo("删除头文件成功！");
			logsSer.addSysLogs("删除头文件【"+docTemplate.getFile_name()+"】路径【"+docTemplate.getFile_path()+"】");

		}
		return goTemplate();
	}
	
	public String downAttach(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}
		if(officialDocPojo.getId() > 0){
			officialDoc = officialDocService.query(officialDocPojo.getId());
			if(StringUtils.isNullOrEmpty(officialDoc.getDoc_file_path())){
				this.setAjaxMsg("该公文没有生成下发文件，请联系系统管理员！");
			}else{
				try{
					String newfilename = officialDoc.getTitle() + ComUtil.getExtention(officialDoc.getDoc_file_name());
					File file = new File(getRealPath() + officialDoc.getDoc_file_path());
					getResponse().setContentType("application/x-msdownload; charset=UTF-8");
					getResponse().setHeader("Content-Disposition", "attachment; filename=\""
							+ new String(newfilename.getBytes("GBK"), "ISO8859_1") + "\"");
					InputStream in = new FileInputStream(file);
					OutputStream out = getResponse().getOutputStream();
					int bufferSize = 1024;
					byte[] buffer = new byte[bufferSize];
					int len = -1;
					while ((len = in.read(buffer)) != -1) {
						out.write(buffer, 0, len);
						out.flush();
					}
					in.close();
					out.close();
					this.setAjaxMsg("success");
				}catch(Exception e){
					e.printStackTrace();
					this.setAjaxMsg("文件下载异常：" + e.getMessage());
				}
			}
		}
		return Action.SUCCESS;
	}

	public OfficialDocManagerService getOfficialDocService() {
		return officialDocService;
	}

	public void setOfficialDocService(OfficialDocManagerService officialDocService) {
		this.officialDocService = officialDocService;
	}

	public OfficialDoc getOfficialDoc() {
		return officialDoc;
	}

	public void setOfficialDoc(OfficialDoc officialDoc) {
		this.officialDoc = officialDoc;
	}

	public List<OfficialDoc> getOfficialDocList() {
		return officialDocList;
	}

	public void setOfficialDocList(List<OfficialDoc> officialDocList) {
		this.officialDocList = officialDocList;
	}

	public OfficialDocPojo getOfficialDocPojo() {
		return officialDocPojo;
	}

	public void setOfficialDocPojo(OfficialDocPojo officialDocPojo) {
		this.officialDocPojo = officialDocPojo;
	}

	public int getOrgid() {
		return orgid;
	}

	public void setOrgid(int orgid) {
		this.orgid = orgid;
	}

	public List<TreeItem> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<TreeItem> orgList) {
		this.orgList = orgList;
	}

	public String getSelSection() {
		return selSection;
	}

	public void setSelSection(String selSection) {
		this.selSection = selSection;
	}

	public List<OfficialDocOrg> getOfficialDocOrgList() {
		return officialDocOrgList;
	}

	public void setOfficialDocOrgList(List<OfficialDocOrg> officialDocOrgList) {
		this.officialDocOrgList = officialDocOrgList;
	}

	public OfficialDocOrgPojo getOfficialDocOrgPojo() {
		return officialDocOrgPojo;
	}

	public void setOfficialDocOrgPojo(OfficialDocOrgPojo officialDocOrgPojo) {
		this.officialDocOrgPojo = officialDocOrgPojo;
	}

	public OfficialDocOrg getOfficialDocOrg() {
		return officialDocOrg;
	}

	public void setOfficialDocOrg(OfficialDocOrg officialDocOrg) {
		this.officialDocOrg = officialDocOrg;
	}

	public List<SealSignature> getSealSignatureList() {
		return sealSignatureList;
	}

	public void setSealSignatureList(List<SealSignature> sealSignatureList) {
		this.sealSignatureList = sealSignatureList;
	}

	public SealSignature getSealSignature() {
		return sealSignature;
	}

	public void setSealSignature(SealSignature sealSignature) {
		this.sealSignature = sealSignature;
	}

	public long getDocAttachId() {
		return docAttachId;
	}

	public void setDocAttachId(long docAttachId) {
		this.docAttachId = docAttachId;
	}

	public String getAjaxMsg() {
		return ajaxMsg;
	}

	public void setAjaxMsg(String ajaxMsg) {
		this.ajaxMsg = ajaxMsg;
	}

	public List<DocTemplate> getDocTemplateList() {
		return docTemplateList;
	}

	public void setDocTemplateList(List<DocTemplate> docTemplateList) {
		this.docTemplateList = docTemplateList;
	}

	public DocTemplate getDocTemplate() {
		return docTemplate;
	}

	public void setDocTemplate(DocTemplate docTemplate) {
		this.docTemplate = docTemplate;
	}

	public long getDocTemplateId() {
		return docTemplateId;
	}

	public void setDocTemplateId(long docTemplateId) {
		this.docTemplateId = docTemplateId;
	}
	public List<String> getAllPower() {
		return allPower;
	}

	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}

	public List<OfficialDocOrg> getOfficialDocOrgList1() {
		return officialDocOrgList1;
	}

	public void setOfficialDocOrgList1(List<OfficialDocOrg> officialDocOrgList1) {
		this.officialDocOrgList1 = officialDocOrgList1;
	}

	public List<OfficialDocOrg> getOfficialDocOrgList2() {
		return officialDocOrgList2;
	}

	public void setOfficialDocOrgList2(List<OfficialDocOrg> officialDocOrgList2) {
		this.officialDocOrgList2 = officialDocOrgList2;
	}

	public List<OrganizationParam> getParamList1() {
		return ParamList1;
	}

	public void setParamList1(List<OrganizationParam> paramList1) {
		ParamList1 = paramList1;
	}

	public List<OrganizationParam> getParamList2() {
		return ParamList2;
	}

	public void setParamList2(List<OrganizationParam> paramList2) {
		ParamList2 = paramList2;
	}

	public List<OrganizationParam> getParamList3() {
		return ParamList3;
	}

	public void setParamList3(List<OrganizationParam> paramList3) {
		ParamList3 = paramList3;
	}

	public List<OrganizationParam> getParamList4() {
		return ParamList4;
	}

	public void setParamList4(List<OrganizationParam> paramList4) {
		ParamList4 = paramList4;
	}

	public OrganizationParamService getParamService() {
		return ParamService;
	}

	public void setParamService(OrganizationParamService paramService) {
		ParamService = paramService;
	}

	public String getReceive_section() {
		return receive_section;
	}

	public void setReceive_section(String receiveSection) {
		receive_section = receiveSection;
	}

	public OfficialDoc getOfficialDoc1() {
		return officialDoc1;
	}

	public void setOfficialDoc1(OfficialDoc officialDoc1) {
		this.officialDoc1 = officialDoc1;
	}

	public List<OrganizationManager> getForglist() {
		return forglist;
	}

	public void setForglist(List<OrganizationManager> forglist) {
		this.forglist = forglist;
	}

	public OrganizationParam getParam() {
		return param;
	}

	public void setParam(OrganizationParam param) {
		this.param = param;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public List<OrganizationManager> getThreelist() {
		return threelist;
	}

	public void setThreelist(List<OrganizationManager> threelist) {
		this.threelist = threelist;
	}

	public DocAttachment getDocAttachment() {
		return docAttachment;
	}

	public void setDocAttachment(DocAttachment docAttachment) {
		this.docAttachment = docAttachment;
	}

	public long getSend_userid() {
		return send_userid;
	}

	public void setSend_userid(long sendUserid) {
		send_userid = sendUserid;
	}

	public OrganizationManager getOrgManager() {
		return orgManager;
	}

	public void setOrgManager(OrganizationManager orgManager) {
		this.orgManager = orgManager;
	}

	public List<OfficialDocOrg> getOfficialDocOrgList3() {
		return officialDocOrgList3;
	}

	public void setOfficialDocOrgList3(List<OfficialDocOrg> officialDocOrgList3) {
		this.officialDocOrgList3 = officialDocOrgList3;
	}

	
	

	
}
